package queuedemo;

import java.util.Stack;

/**
 * 用栈实现队列
 */

class MyQueue {
    private Stack<Integer> s1;
    private Stack<Integer> s2;


    public MyQueue() {
        s1=new Stack<>();
        s2=new Stack<>();
    }
    //入队逻辑：把元素放到第一个栈当中
    
    public void push(int x) {
        s1.push(x);

    }
    //如果第二个栈不为空，则栈顶出栈。否则，将第一个栈的全部元素放到第二个栈中栈顶出栈
    
    public int pop() {
        if(empty()){
            return -1;
        }
        if(s2.empty()){
            //那么就把s1里面放到s2里
            while(!s1.empty()){
                s2.push(s1.pop());
            }

        }
        return s2.pop();

    }
    
    public int peek() {
        if(empty()){
            return -1;
        }
        if(s2.empty()){
            //那么就把s1里面放到s2里
            while(!s1.empty()){
                s2.push(s1.pop());
            }

        }
        return s2.peek();


    }
    
    public boolean empty() {
        return s1.empty()&&s2.empty();

    }
}

/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */